//
//  WYHttpServer+WYUserMessage.m
//  Boss
//
//  Created by admin on 2018/6/11.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYHttpServer+WYUserMessage.h"

@implementation WYHttpServer (WYUserMessage)
+(void)sendMessageWithPhone:(NSString *)phone SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:phone forKey:@"phone"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlSendMessage WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}
+(void)masterLoginWithUserName:(NSString *)phone WithCode:(NSString *)code SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:phone forKey:@"phone"];
    [parm setValue:code forKey:@"code"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlLogin WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
    
    
}
+(void)uploadImageSucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:@"JPEG" forKey:@"fileType"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetCOSUploadToken WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}
+(void)bindingWechatWithPhone:(NSString *)phone WithCode:(NSString *)code SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:phone forKey:@"phone"];
    [parm setValue:code forKey:@"code"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlBindWeChat WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
    
    
    
    
    
    
}






+(void)upLoadImage:(NSData *)data WithUrl:(NSString *)url SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"file"]=@"headImage.png";
    //    NSString *messStr=[NSString stringWithFormat:@"%@%@",JJUDURL,JJUDuserInfosave];
    //formData: 专门用于拼接需要上传的数据,在此位置生成一个要上传的数据体
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //        UIImage *image =[UIImage imageNamed:@"moon"];
        // NSData *data = UIImagePNGRepresentation(image);
        // 在网络开发中，上传文件时，是文件不允许被覆盖，文件重名
        // 要解决此问题，
        // 可以在上传时使用当前的系统事件作为文件名
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmsssss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
        //上传
        /*
         此方法参数
         1. 要上传的[二进制数据]
         2. 对应网站上[upload.php中]处理文件的[字段"file"]
         3. 要保存在服务器上的[文件名]
         4. 上传文件的[mimeType]
         */
        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //上传进度
        // @property int64_t totalUnitCount;     需要下载文件的总大小
        // @property int64_t completedUnitCount; 当前已经下载的大小
        //
        // 给Progress添加监听 KVO
        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
        // 回到主队列刷新UI,用户自定义的进度条
        //        dispatch_async(dispatch_get_main_queue(), ^{
        //            self.progressView.progress = 1.0 *
        //            uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
        //        });
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        [self reloadview];
        //        NSLog(@"上传成功 %@", responseObject);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
        if (![dic isKindOfClass:[NSDictionary class]]) {
            HUDNormal(@"服务器闹点小情绪,努力安抚中,请稍候~");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenLoading" object:nil];
            return;
        }
        
        NSLog(@"%@",dic);
        
        
        if ([dic[@"code"] integerValue]==0) {
            completion(YES,dic,nil);
        }else{
            completion(NO,nil,dic[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completion(NO,nil,@"上传失败");
        NSLog(@"上传失败 %@", error);
    }];
}




+ (void)PUTWithURLStr:(NSString *)urlStr
             filePath:(NSString *)filePath SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    // 1. url 最后一个是要上传的文件名
    NSURL *url = [NSURL URLWithString:urlStr];
    // 2. request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"PUT";
    // 3. URLSession
    NSURLSession *session = [NSURLSession sharedSession];
    // 4. 由session发起任务
    NSURL *localURL = [NSURL URLWithString:filePath];
    [[session uploadTaskWithRequest:request fromFile:localURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"sesult---> %@ %@", result, [NSThread currentThread]);
        
        if (!error) {
            completion(YES,result,nil);
        }else{
            completion(NO,result,nil);
        }
        
    }] resume];
    
    
    
    
    
}


+(void)donePersonalMessageWithSex:(NSString *)sex WithBirthday:(NSString *)birthday WithProvince:(NSString *)province WithCity:(NSString *)city WithPrefecture:(NSString *)prefecture SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    
    
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
     [parm setValue:sex forKey:@"sex"];
     [parm setValue:birthday forKey:@"birthday"];
     [parm setValue:province forKey:@"province"];
     [parm setValue:city forKey:@"city"];
    [parm setValue:prefecture forKey:@"prefecture"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlImprovePersonal WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}

+(void)getShareRedPacketSucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlCreateSharePacket WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}

+(void)getNewMyInfoSucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlUserMyInfoNew WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
    
    
}




@end
